/*
 * GoodsDetailPresenter.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.goodsdetail;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.ShareEntity;
import com.yidiandao.business.data.event.RefreshSecondKillEvent;
import com.yidiandao.business.data.model.CollectionModel;
import com.yidiandao.business.data.model.GoodsModel;
import com.yidiandao.business.data.model.ICollectionModel;
import com.yidiandao.business.data.model.IGoodsModel;
import com.yidiandao.business.data.response.GoodsDetailResponse;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * 商品详情页Presenter.
 */
public class GoodsDetailPresenter implements GoodsDetailContract.Presenter {

    private final GoodsModel mGoodsModel;
    private final CollectionModel mCollectionModel;
    boolean hasCollect = false; //收藏
    private GoodsDetailContract.View mGoodsDetailView;
    private Goods mGoods;

    private ShareEntity mShareEntity;

    private int mCartNum; //加入购物车的数量

    private boolean mCanBuy;

    private boolean isRefreshing;

    public GoodsDetailPresenter(GoodsDetailContract.View view) {
        mGoodsDetailView = view;

        mGoodsDetailView.setPresenter(this);
        mGoodsModel = new GoodsModel();
        mCollectionModel = new CollectionModel();
    }

    @Override
    public void start() {
        if (isRefreshing) {
            return;
        }

        getGoodsDetail(mGoodsDetailView.getGoodsId());
    }

    @Override
    public void getGoodsDetail(String goodsId) {
        isRefreshing = true;
        mGoodsDetailView.showDialog();
        mGoodsModel.getGoodsDetail(
                mGoodsDetailView.getPagerTag(),
                goodsId,
                new IGoodsModel.onGetGoodsDataListener() {

                    @Override
                    public void onSuccess(GoodsDetailResponse.DataBean dataBean) {
                        mGoods = dataBean.getGoodsInfo();
                        mShareEntity = dataBean.getShare();

                        //加入购物车的数量
                        mCartNum = dataBean.getCartGoodsNumber();
                        mGoodsDetailView.setCartStyle(mCartNum);
                        //此物品是否已收藏
                        int collect = dataBean.getHasCollect();
                        hasCollect = (collect != 0);
                        mGoodsDetailView.setCollectStyle(hasCollect);
                        mGoodsDetailView.setViewVisible();
                        //设置viewpager的数据
                        mGoodsDetailView.setViewPager(dataBean);
                        //商品的下架判断  0 下架
                        mCanBuy = mGoods.getCanBuy() == 1;
                        mGoodsDetailView.setCanBuyStatus(mCanBuy);
                        //设置其他数据
                        mGoodsDetailView.setGoodsInfo(dataBean);
                    }

                    @Override
                    public void onFailed() {
                        mGoodsDetailView.setViewGone();
                    }

                    @Override
                    public void onFinal() {
                        mGoodsDetailView.dismissDialog();
                        isRefreshing = false;
                    }
                });
    }

    @Override
    public void collectGoods(String goodsId, final boolean hasCollect) {
        if (!hasCollect && !mCanBuy) {
            UIUtils.showToast(UIUtils.getString(R.string.goodsdetail_cannot_buy));
            return;
        }
        int type = hasCollect ? 1 : 0;
        mCollectionModel.collectionGoods(
                mGoodsDetailView.getPagerTag(),
                goodsId,
                type,
                new ICollectionModel.OnAccessNetListener() {
                    @Override
                    public void onSuccess() {
                        UIUtils.showToast(
                                UIUtils.getString(
                                        hasCollect
                                                ? R.string.shop_cancel_collect_success
                                                : R.string.shop_collect_success));
                        mGoodsDetailView.setCollectStyle(!hasCollect);
                    }
                });
    }

    @Override
    public void addToCart(final String goodsId, final int count) {
        mGoodsDetailView.showDialog();
        mGoodsModel.addToCart(
                mGoodsDetailView.getPagerTag(),
                goodsId,
                count,
                new IGoodsModel.OnAddToShoppingCartListener() {

                    @Override
                    public void onSuccess(String recId, int goodsNumber, float goodsPrice) {
                        mCartNum = count;
                        UIUtils.showToast(UIUtils.getString(R.string.goodsdetail_tip2));
                        mGoodsDetailView.setCartStyle(count);
                    }

                    @Override
                    public void onFinal() {
                        mGoodsDetailView.dismissDialog();
                    }
                });
    }

    @Override
    public void buyNow(String goodsId, int count) {
        mGoodsDetailView.showDialog();
        mGoodsModel.buyNow(mGoodsDetailView.getPagerTag(), goodsId, count, new IGoodsModel.onBuyNowListener() {
            @Override
            public void onSuccess(String json, String recId, int goodsNumber, float goodsPrice) {
                mGoodsDetailView.toConfirmOrderActivity(json);
            }

            @Override
            public void onFailed() {
            }

            @Override
            public void onFinal() {
                mGoodsDetailView.dismissDialog();
            }
        });
    }

    public ShareEntity getShareEntity() {
        return mShareEntity;
    }

    @Override
    public void postRefreshHomeSecondKillEvent() {
        if (mGoods != null && mGoods.getStockNumber() == 0) {
            // 商品售罄，通知刷新首页秒杀数据
            EventBus.getDefault().post(new RefreshSecondKillEvent());
        }
    }
}
